

'type set containing entity templates in a bag, by ID
Type TEntityTemplateSet

	'friendly name for this set
	'only used in editor
	Field _label:String

	Field _templateSetID:Int
	
	'images groupname for this set in TResources
	Field _resourceGroupName:String
	
	'filename by which this set was loaded
	Field _fileName:String
	
	'bag containing all templates for this set
	'index = id
	Field _templates:TBag

	
	
	Method New()
		_templates = New TBag
        _label="TemplateSet"
	End Method
	
	
	
	rem
	bbdoc: Reads entity set from a stream.
	endrem
	Method Read:Int(stream:TStream, index:Int)
		_templateSetID = index
		_resourceGroupName = "SET" + index
		Local line:String = stream.ReadLine()
		While Not stream.Eof()
			If line = "[common]" Then Self.LoadCommon(stream)
			If line = "[images]" Then Self.LoadImages(stream)
			If line = "[object]" Then Self.LoadTemplate(stream)
			line = stream.ReadLine()
		Wend
	End Method

	
	'load set common settings
	Method LoadCommon:Int(s:TStream)
		Local line:String = s.ReadLine()
		While line <> "[/common]"
			Local splittedLine:String[] = line.Split("=")
			Select splittedLine[0]
				Case "setname"
					_label = splittedLine[1]
			End Select
			line = s.ReadLine()
		Wend
	End Method
	
	
	'images are stored in the resources manager
	'groupname has been set in the Read() method.
	Method LoadImages:Int(s:TStream)
		Local line:String = s.ReadLine()
		While line <> "[/images]"
			Local splittedLine:String[] = line.Split(",")
			Select splittedLine.Length
				Case 2
					'single image
					TResources.StoreImage(LoadImage(splittedLine[0]), splittedLine[1], _resourceGroupName)
				Case 5
					'image with frames
					TResources.StoreImage(LoadAnimImage(splittedLine[0], Int(splittedLine[2]),  ..
						Int(splittedLine[3]), 0, Int(splittedLine[4])), splittedLine[1], _resourceGroupName)
			End Select
			line = s.ReadLine()
		Wend
	End Method

	
	
	'this method continues to load settings until [/object] has been read.
	'must run after _label has been set.
	Method LoadTemplate(s:TStream)
		Local t:TEntityTemplate = New TEntityTemplate
		Local line:String = s.ReadLine()
		While line <> "[/object]"
			Local splittedLine:String[] = line.Split("=")
			If splittedLine.Length = 2
			
				'add the _resourceGroupName to renderform setting
				If splittedLine[0] = "renderform"
					splittedLine[1]:+"," + _resourceGroupName
				End If
			
				t.AddComponentSetting(splittedLine[0], splittedLine[1])
			Else
				RuntimeError("error in object definition")
			EndIf
			line = s.ReadLine()
		Wend
		
		'the template id is included in the template file.
		Local index:Int = Int(t.GetComponentSetting("id"))
		
		'let template know which id is has in this set.
		t._templateID = index
		
		'store template in this set.
		_templates.Set(index, t)
	End Method
    
	
    
    Method GetLabel:String()
        Return _label
    End Method
    
	
	
	Method GetFilename:String()
		Return _fileName
	End Method
	
	
	
	Method SetFilename(newName:String)
		_fileName = newName
	End Method
	
	
    
	'returns size of template set.
    Method GetSize:Int()
        Return _templates.GetSize()
    End Method
	
	
	rem
	bbdoc: Returns entity template by index.
	endrem
	Method Get:TEntityTemplate(index:Int)
		Return TEntityTemplate(_templates.Get(index))
	End Method
	
	
	rem
	bbdoc: Clones template by index to entity.
	endrem
	Method CloneToWorldbyIndex:TEntity(index:Int, world:TWorld, layer:Int)
		Local t:TEntityTemplate = Self.Get(index)
		Return t.CloneToEntity(world, layer, _templateSetID)
	End Method

	rem
	bbdoc: Clones passed template.
	endrem
	Method CloneToWorld:TEntity(t:TEntityTemplate, world:TWorld, layer:Int)
		Return t.CloneToEntity(world, layer, _templateSetID)
	End Method

		
End Type

